// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.population;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import zombie.characters.HairOutfitDefinitions;
import zombie.core.logger.ExceptionLogger;
import zombie.debug.DebugLog;
import zombie.debug.DebugType;
import zombie.gameStates.ChooseGameInfo;

@XmlRootElement
public class BeardStyles {
    @XmlElement(
        name = "style"
    )
    public final ArrayList<BeardStyle> m_Styles = new ArrayList();
    @XmlTransient
    public static BeardStyles instance;

    public static void init() {
        instance = Parse(
            zombie.ZomboidFileSystem.instance.base.getAbsolutePath()
                + File.separator
                + zombie.ZomboidFileSystem.processFilePath("media/hairStyles/beardStyles.xml", File.separatorChar)
        );
        if (instance != null) {
            instance.m_Styles.add(0, new BeardStyle());

            for (String string0 : zombie.ZomboidFileSystem.instance.getModIDs()) {
                ChooseGameInfo.Mod mod = ChooseGameInfo.getAvailableModDetails(string0);
                if (mod != null) {
                    String string1 = zombie.ZomboidFileSystem.instance.getModDir(string0);
                    BeardStyles beardStyles = Parse(
                        string1 + File.separator + zombie.ZomboidFileSystem.processFilePath("media/hairStyles/beardStyles.xml", File.separatorChar)
                    );
                    if (beardStyles != null) {
                        for (BeardStyle beardStyle0 : beardStyles.m_Styles) {
                            BeardStyle beardStyle1 = instance.FindStyle(beardStyle0.name);
                            if (beardStyle1 == null) {
                                instance.m_Styles.add(beardStyle0);
                            } else {
                                if (DebugLog.isEnabled(DebugType.Clothing)) {
                                    DebugLog.Clothing.println("mod \"%s\" overrides beard \"%s\"", string0, beardStyle0.name);
                                }

                                int _int = instance.m_Styles.indexOf(beardStyle1);
                                instance.m_Styles.set(_int, beardStyle0);
                            }
                        }
                    }
                }
            }
        }
    }

    public static void Reset() {
        if (instance != null) {
            instance.m_Styles.clear();
            instance = null;
        }
    }

    public static BeardStyles Parse(String filename) {
        try {
            return parse(filename);
        } catch (FileNotFoundException fileNotFoundException) {
        } catch (IOException | JAXBException jAXBException) {
            ExceptionLogger.logException(jAXBException);
        }

        return null;
    }

    public static BeardStyles parse(String filename) throws JAXBException, IOException {
        FileInputStream fileInputStream = new FileInputStream(filename);

        BeardStyles beardStyles;
        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(BeardStyles.class);
            Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
            beardStyles = (BeardStyles)unmarshaller.unmarshal(fileInputStream);
        } catch (Throwable throwable0) {
            try {
                fileInputStream.close();
            } catch (Throwable throwable1) {
                throwable0.addSuppressed(throwable1);
            }

            throw throwable0;
        }

        fileInputStream.close();
        return beardStyles;
    }

    public BeardStyle FindStyle(String name) {
        for (int _int = 0; _int < this.m_Styles.size(); _int++) {
            BeardStyle beardStyle = (BeardStyle)this.m_Styles.get(_int);
            if (beardStyle.name.equalsIgnoreCase(name)) {
                return beardStyle;
            }
        }

        return null;
    }

    public String getRandomStyle(String outfitName) {
        return HairOutfitDefinitions.instance.getRandomBeard(outfitName, this.m_Styles);
    }

    public BeardStyles getInstance() {
        return instance;
    }

    public ArrayList<BeardStyle> getAllStyles() {
        return this.m_Styles;
    }
}
